package com.zhangyu.shadow.repair.delegate.history;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.CommonBean;
import com.zhangyu.shadow.repair.delegate.bean.VehicleBean;
import com.zhangyu.shadow.repair.intercept.DataIntercept;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.recycler.ObjConverter;

/**
 * @author zhangyu
 * @date 2018/12/4
 */

public class HistoryModel implements HistoryContract.Model {
    @Override
    public Disposable getHistoryList(String content,
                                     final IStringDataConvert<ArrayList<VehicleBean>> convert,
                                     final IError error) {

        return RxRestClient.builder()
                .url(RequestUrls.URL_HISTORY_DATA_LIST)
                .body(RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                        content))
                .interceptor(new DataIntercept())
                .build()
                .flatPostRaw()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<String>() {

                    @Override
                    public void onNext(String response) {
                        JSONObject jsonObject = JSONObject.parseObject(response);
                        int resultCode = jsonObject.getInteger("resultCode");
                        String object = jsonObject.getString("result");
                        String resultMsg = jsonObject.getString("resultMsg");
                        if (resultCode == RxRestClient.SUCCESS_CODE) {
                            convert.convert(convertBusListBean(object));
                        } else {
                            error.onError(resultCode, resultMsg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public Disposable getMaintainType(final IStringDataConvert<ArrayList<MaintainType>> convert,
                                      IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_MAINTAIN_TYPE)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("queryParam",AccountManager.getInstance().getOrgId())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertMaintainList(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable getRepairType(final IStringDataConvert<ArrayList<MaintainType>> convert,
                                    IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_REPAIR_TYPE)
                .params(AccountManager.getInstance().getBaseInfo())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertMaintainList(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable getClassification(final IStringDataConvert<ArrayList<CommonBean>> convert,
                                        IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_GET_CLASSIFICATION)
                .params(AccountManager.getInstance().getBaseInfo())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertClassificationBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    private ArrayList<VehicleBean> convertBusListBean(String response) {
        return new ObjConverter<ArrayList<VehicleBean>>().convert(response
                , new TypeReference<ArrayList<VehicleBean>>() {
                });
    }

    private ArrayList<CommonBean> convertClassificationBean(String response) {

        return new ObjConverter<ArrayList<CommonBean>>().convert(response
                , new TypeReference<ArrayList<CommonBean>>() {
                });
    }

    private ArrayList<MaintainType> convertMaintainList(String response) {
        ArrayList<String> list = new ObjConverter<ArrayList<String>>().convert(response
                , new TypeReference<ArrayList<String>>() {
                });
        ArrayList<MaintainType> maintainTypes = new ArrayList<>();
        if (list != null) {
            for (String str : list) {
                maintainTypes.add(new MaintainType(str, false));
            }
        }
        return maintainTypes;
    }

}
